package com.graph;

import java.util.LinkedList;

public class Solution1306 {
    public boolean canReach(int[] arr, int start) {
        if (arr[start] == 0) return true;
        LinkedList<Integer> queue = new LinkedList<>();
        queue.add(start);
        boolean[] visited = new boolean[arr.length];
        visited[start] = true;
        while (!queue.isEmpty()) {
            int cur = queue.removeFirst();
            for (int i = 1; i <= arr[cur]; i++) {
                int pre = cur - arr[cur];
                int post = cur + arr[cur];
                if (post < arr.length && !visited[post]) {
                    if (arr[post] == 0) return true;
                    queue.add(post);
                    visited[post] = true;
                }
                if (pre >= 0 && !visited[pre]) {
                    if (arr[pre] == 0) return true;
                    queue.add(pre);
                    visited[pre] = true;
                }
            }
        }
        return false;
    }
}
